package com.home766.controller.pad;

import com.home766.dto.DictionaryDTO;
import com.home766.entity.Dictionary;
import com.home766.entity.Scheme;
import com.home766.entity.SchemeProduct;
import com.home766.service.IDictionaryService;
import com.home766.service.SchemeProductService;
import com.home766.service.SchemeService;
import com.home766.util.StringUtil;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.controller.shop.BaseController;
import net.biz.util.ParamsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * pad方案-controller
 */
@Controller
@RequestMapping("/pad/scheme")
public class PadSchemeController extends BaseController {
	
	@Resource(name="schemeServiceImpl")
	private SchemeService schemeService;
	@Resource(name="schemeProductServiceImpl")
	private SchemeProductService schemeProductService;
	@Autowired
	private IDictionaryService iDictionaryService;
	
	/**
	 * 方案列表
	 * @param pageable
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(Pageable pageable, ModelMap model, Integer pageNumber, Long typeId, Integer isPriceAsc){
		if(pageNumber != null){
			pageable.setPageNumber(pageNumber);
		}
		pageable.setPageSize(ParamsUtil.PAD_PAGE_PAGESIZE);
		Page<Scheme> page = schemeService.findPage(pageable, null, null, null, null, null, typeId, true,null);
		List<DictionaryDTO> schemeTypes = iDictionaryService.searchChildren(ParamsUtil.SCHEME_TYPE, true);
		model.addAttribute("page", page);
		model.addAttribute("schemeTypes", schemeTypes);
		model.addAttribute("typeId", typeId);
		return "/pad/scheme/list";
	}
	
	/**
	 * 方案详情
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/content",method = RequestMethod.GET)
	public String content(ModelMap model, Long id){
		if(id == null){
			return PAD_ERROR_VIEW;
		}
		Scheme scheme = schemeService.find(id);
		if(scheme == null){
			return PAD_ERROR_VIEW;
		}
		model.addAttribute("scheme",scheme);
		Dictionary type = iDictionaryService.find(scheme.getType());
		model.addAttribute("schemeType", type);
		List<Long> attrs = new ArrayList<Long>();
		if(!StringUtil.isNullOrSpace(scheme.getAttribute())){
			String[] sattr = scheme.getAttribute().split(",");
			for(String s : sattr){
				attrs.add(Long.parseLong(s));
			}
			model.addAttribute("attrs", attrs);
		}
		//查询对应类型的属性值
		List<DictionaryDTO> schemeAttrs = iDictionaryService.searchChildren(ParamsUtil.SCHEME_ATTRIBUTE);
		model.addAttribute("schAttrs", schemeAttrs);
		model.addAttribute("typeIdZNKT", ParamsUtil.SCHEME_PRODUCT_ID_ZNKT);
		model.addAttribute("typeIdZNNQ", ParamsUtil.SCHEME_PRODUCT_ID_ZNNQ);
		model.addAttribute("categoryIdDL", ParamsUtil.SCHEME_PRODUCT_ID_DL);
		model.addAttribute("mingzhuangId", ParamsUtil.SCHEME_PRODUCT_ID_ZNMZ);
		model.addAttribute("anzhuangId", ParamsUtil.SCHEME_PRODUCT_ID_ZNAZ);
		return "/pad/scheme/content";
	}
	
	/**
	 * 
	 * @param model
	 * @param categoryId
	 * @return
	 */
	@RequestMapping(value="/findByCategoryId",method = RequestMethod.POST)
	@ResponseBody
	public List<SchemeProduct> content(Long categoryId){
		if(categoryId == null){
			return null;
		}
		//查询该方案类型对应的所有主材产品
		List<SchemeProduct> schemeProducts = schemeProductService.findByCategory(categoryId,true);
		return schemeProducts;
	}
	
}
